package com.xuecheng.publish.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ErrorCoursePubMsgConfig {

    /*
    * course.publish.error.exchange = error.course_pub.direct
    course.publish.error.queue = error.course_page.queue
    course.publish.error.routingkey = error.publish_course
    * */
    @Value("${course.publish.error.exchange}")
    private String errorExchangeName;
    @Value("${course.publish.error.queue}")
    private String errorqueueName;
    @Value("${course.publish.error.routingkey}")
    private String errorroutingkey;

    @Bean
    public DirectExchange errorMessageExchange() {
        return new DirectExchange(errorExchangeName);
    }

    @Bean
    public Queue errorQueue() {
        return new Queue(errorqueueName, true);
    }

    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange errorMessageExchange) {
        return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with(errorroutingkey);
    }

    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, errorExchangeName, errorroutingkey);
    }
}